<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="com.cap.bts.framework.usermgnt.constant.EmployeeStatus"%>    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>创建会员卡配送信息</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css"/>
<link rel="stylesheet" type="text/css" href="${csspath }/datepicker.css"/>
<link rel="stylesheet" type="text/css" href="${csspath }/style_l.css"/>
<script type="text/javascript" src="${jspath }/jquery-1.4.4.js"></script>
<script type="text/javascript" src="${jspath }/jquery.ui.min.js"></script>
<script type="text/javascript" src="${jspath }/ui.datepicker_zh-CN.js"></script>
<script type="text/javascript" src="${jspath }/jquery.blockUI.js"></script>
<script type="text/javascript" src="${jspath }/public.js"></script>
<script type="text/javascript" src="${jspath }/jquery.selectAll.js"></script>
<script type="text/javascript" src="${jspath}/jquery/jquery.ui.draggable.js"></script>
<script type="text/javascript" src="${jspath}/jquery/jquery.alerts.customerize.js"></script>
<script type="text/javascript" src="${jspath}/load_layer.js"></script>
<link rel="stylesheet" type="text/css" href="${csspath}/jquery.alerts.css" />

<link rel="stylesheet" type="text/css"
	href="${webcontext}/css/validationEngine.jquery.css" />
<script src="${jspath}/jquery/validate/jquery.validationEngine.js"
	type="text/javascript"></script>
<script src="${jspath}/jquery/validate/jquery.validationEngine-zh.js"
	type="text/javascript"></script>
	<script type="text/javascript"
	src="${webcontext}/javascript/jquery/jquery.ui.draggable.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<style type="text/css">
.formError .formErrorContent {
    -moz-border-radius: 6px 6px 6px 6px;
    -moz-box-shadow: 0 0 6px #000000;
    background: none repeat scroll 0 0 #EE0101;
    border: 2px solid #DDDDDD;
    color: #FFFFFF;
    font-family: tahoma;
    font-size: 11px;
    padding: 4px 10px;
    width: 100px;
}
a {
display:block;
width:100px;
height:40px;
border:1px solid #CCC;
background:#FFF;
}
</style>

<script type="text/javascript">
var myHeight = 0;
$(function(){
	$("#search_form").validationEngine(
			{
             promptPosition:"topLeft",
             success:false,
             validationEventTriggers:"blur",
             scroll : false,
			 failure:function(){} 
	});
	if("${flag}" == "dealerNoWrong"){
		jAlert('门店编号错误。', '提示');
	}
	if("${flag}" == "canNotAdd"){
		jAlert('商品库存不足，添加失败。', '提示');
	}

	$("#dealerNo").blur(function(){
		$.ajax({
			type:'POST',
			url: '${webcontext}/crmdp/dealer/logistic/toCheckDealer',
			async: false,
			data : $('#dealerNo').serialize(),
			dataType : 'json',
			success : function(data) {
				if(data){
					$('#errorSpan').html("门店编号输入错误");
				}else{
					$('#errorSpan').html("");
				}
			}
		})
	}) 
	
	if("${flag}" == "addSuccess"){
		jConfirm("操作成功,是否继续添加？","确认提示",function(result){
    		if(result == false){	
    			info_form.action="${webcontext}/crmdp/dealer/logistic/backToLogisitcPage";
    			document.getElementById("info_form").submit();
    		}
    	});
	}
	
	var today = new Date();   
	var day = today.getDate();   
	var month = today.getMonth() + 1;   
	var year = today.getFullYear();    
	var date = year + "-" + month + "-" + day;
	document.getElementById("shipDate").value = date;


});
  
function check(){
	var dealerNo = document.getElementById("dealerNo").value;
	var errorSpan = document.getElementById("errorSpan").innerHTML;
	var expressCom = document.getElementById("expressCom").value;
	var expressNo = document.getElementById("expressNo").value;
	if(dealerNo == "" || dealerNo == undefined || dealerNo == null){
		jAlert('请输入门店编号', '提示');
		return false;
	} 
	if(errorSpan != "" && errorSpan != undefined && errorSpan != null){
		jAlert('门店编号输入错误。', '提示');
		return false;
	}
	/* if(expressCom == "" || expressCom == undefined || expressCom == null){
		jAlert('请输入物流公司');
		return false;
	} 
	if(expressNo == "" || expressNo == undefined || expressNo == null){
		jAlert('请输入物流单号');
		return false;
	}  */
	var shipDate = $("#shipDate").val();
	var estimatedArriveDate = $("#estimatedArriveDate").val();
	var startD = new Date(Date.parse(shipDate.replace(/-/g,"/")));
	var endD   = new Date(Date.parse(estimatedArriveDate.replace(/-/g,"/")));
	var days = parseInt((endD.getTime()-startD.getTime()) / (1000 * 60 * 60 * 24));
	
	if(shipDate == "" || shipDate == undefined || shipDate == null){
		jAlert('请输入发货时间', '提示');
		return false;
	}
	if(estimatedArriveDate == "" || estimatedArriveDate == undefined || estimatedArriveDate == null){
		jAlert('请输入预计到货时间', '提示');
		return false;
	}
	if(!(estimatedArriveDate == "" || estimatedArriveDate == undefined || estimatedArriveDate == null)){
		if(days < 0){
			jAlert('预计到货日期不得早于发货日期', '提示');
			return false;
		}
	}
	var shipAmount=document.getElementsByName('shipAmount');
	var amount=0;
	var reg=/^[0-9]+$/;
	for (var i=0;i<shipAmount.length;i++ ){
		if(shipAmount[i].value!=""&&shipAmount[i].value!=undefined&&shipAmount[i].value!=null){
			if (!reg.test(parseInt(shipAmount[i].value))){
				jAlert('实发数量必须为正整数', '提示');
				return false;
			}
			amount+=parseInt(shipAmount[i].value);
		}
	}
	if(amount == 0){
		jAlert('实发数量不能全为0', '提示');
		return false;
	}
	return true;
	}
function add() {
	if(check()){
		var cards=document.getElementsByName('cards');
		var cardsType=document.getElementsByName('cardsType');
		var shipAmount=document.getElementsByName('shipAmount');
		var cardList="";
		for (var i=0;i<cards.length;i++ ){
			if(shipAmount[i]!=""){
				if(parseInt(shipAmount[i].value) > 0){
					cardList=cardList+cards[i].value + ","+cardsType[i].value + "," + shipAmount[i].value + ";";
					}
				}
			}
		var dealerNo=$("#dealerNo").val();
		var orderNo=$("#orderNo").val();
		var shipDate=$("#shipDate").val();
		var estimatedArriveDate=$("#estimatedArriveDate").val();
		var expressCom=encodeURI(encodeURI($("#expressCom").val()));
		var expressNo=$("#expressNo").val();
		window.location.href = "${webcontext}/crmdp/dealer/logistic/toAddLogisitcPage?cardList="
				+ cardList
				+ "&dealerNo="
				+ dealerNo
				+ "&orderNo="
				+ orderNo
				+ "&shipDate="
				+ shipDate
				+ "&estimatedArriveDate="
				+ estimatedArriveDate
				+ "&expressCom="
				+ expressCom
				+ "&expressNo="
				+ expressNo;
	}
}
function backToSearch(){
	info_form.action="${webcontext}/crmdp/dealer/logistic/backToLogisitcPage";
	document.getElementById("info_form").submit();
}

</script> 
</head>
<body>
			<!-- content -->
	<div id="content">
		    <div class="spliter h_5"></div>
			<div class="page_info">
			</div>
			          <div class="spliter h_5"></div>
		<!-- wrap -->
		<div id="wrap">
			<!-- info_div -->
	<form:form id="info_form" method="post" modelAttribute="dto"><br></br>
	
		<span class="need">*</span>门店编号：<form:input path="dealerNo" id="dealerNo" 
		cssClass="validate[length[0,100]]  text string" 
		name="dealer.dealerNo"/>
		<span id="errorSpan" style="color:red;"></span>
		<br></br>
		
		&nbsp;订单编号：<form:input path="orderNo"  id="orderNo"
		cssClass="validate[length[0,100]]  text string"/><br></br>
		<span class="need">*</span>发货日期：<form:input path="shipDate" id="shipDate" 
					  	cssClass="validate[custom[date]] text date" />&nbsp;&nbsp;&nbsp;
		<span class="need">*</span>预计到货：<form:input path="estimatedArriveDate" id="estimatedArriveDate" 
					  	cssClass="validate[custom[date]] text date" /><br></br>
		&nbsp;物流公司：<form:input path="expressCom" 
		cssClass="validate[length[0,100]]  text string"/>&nbsp;&nbsp;&nbsp;&nbsp;
		物流单号：<form:input path="expressNo" 
		cssClass="validate[length[0,100]]  text string"/><br></br>
		<hr align=center width=500 color=#987cb9 SIZE=1><br>
		<table class="search_tab">
					<tr>
						<td style="width: 160px;">商品名称</td>
					  	<td>实发数量</td>
					  	<td class="label"></td><td class="label"></td><td class="label"></td><td class="label"></td>
					  	<td class="label"></td><td class="label"></td><td class="label"></td><td class="label"></td>
					 </tr>
						<core:forEach items="${cardName}" var="data">
					 <tr>
						<td style="width: 160px;"><input type="hidden" name="cards" value="${data.id}" readonly="readonly"
						style="border:1px;border-bottom-style:none;border-top-style:none;border-left-style:none;border-right-style:none;"/>
						<input type="hidden" name="cardsType" value="${data.type}" readonly="readonly"
						style="border:1px;border-bottom-style:none;border-top-style:none;border-left-style:none;border-right-style:none;"/>
						${data.cardName}</td>
						<td><input name="shipAmount" value="${data.amount}"/></td>
						<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
					</tr>
                 		</core:forEach>
                 		<core:forEach items="${smsName}" var="data">
					 <tr>
						<td style="width: 160px;"><input type="hidden" name="cards" value="${data.id}" readonly="readonly"
						style="border:1px;border-bottom-style:none;border-top-style:none;border-left-style:none;border-right-style:none;"/>
						<input type="hidden" name="cardsType" value="${data.type}" readonly="readonly"
						style="border:1px;border-bottom-style:none;border-top-style:none;border-left-style:none;border-right-style:none;"/>
						${data.cardName}</td>
						<td><input name="shipAmount" value="${data.amount}"/></td>
						<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
					</tr>
                 		</core:forEach>
				</table>
		<br>
		<hr align=center width=500 color=#987cb9 SIZE=1><br>
		<button type="button" class="btn" value="添加" onclick="add()"><i class="glyphicon glyphicon-plus" style="color:#FFFFFF"></i>&nbsp;添加</button>
		&nbsp;&nbsp;
		<button type="button" class="btn" value="返回 " onclick="backToSearch();"><i class="glyphicon glyphicon-share-alt" style="color:#FFFFFF"></i>&nbsp;返回</button>	
  </form:form>
		</div>
		<!-- /wrap -->
		</div>
	<!-- /content -->
  </body>
</html>